package com.itboy.mall.wms.warepurchase.controller;

import java.util.List;
import java.util.Arrays;

import com.itboy.mall.wms.warepurchase.domain.MallWarePurchase;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.itboy.common.annotation.RepeatSubmit;
import com.itboy.common.annotation.Log;
import com.itboy.common.core.controller.BaseController;
import com.itboy.common.core.domain.AjaxResult;
import com.itboy.common.enums.BusinessType;
import com.itboy.mall.wms.warepurchase.vo.MallWarePurchaseVo;
import com.itboy.mall.wms.warepurchase.bo.MallWarePurchaseQueryBo;
import com.itboy.mall.wms.warepurchase.bo.MallWarePurchaseAddBo;
import com.itboy.mall.wms.warepurchase.bo.MallWarePurchaseEditBo;
import com.itboy.mall.wms.warepurchase.service.IMallWarePurchaseService;
import com.itboy.common.utils.poi.ExcelUtil;
import com.itboy.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 采购信息Controller
 *
 * @author itboy
 * @date 2021-08-10
 */
@Validated
@Api(value = "采购信息控制器", tags = {"采购信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mall/warePurchase")
public class MallWarePurchaseController extends BaseController {

    private final IMallWarePurchaseService iMallWarePurchaseService;

    /**
     * 查询采购信息列表
     */
    @ApiOperation("查询采购信息列表")
    @PreAuthorize("@ss.hasPermi('mall:warePurchase:list')")
    @GetMapping("/list")
    public TableDataInfo<MallWarePurchaseVo> list(@Validated MallWarePurchaseQueryBo bo) {
        return iMallWarePurchaseService.queryPageList(bo);
    }

	/**
	 * 查询未分配的采购单
	 */
	@ApiOperation("查询未分配的采购单")
	@GetMapping("/unreceive")
	public AjaxResult<List<MallWarePurchase>> unreceive() {
		return AjaxResult.success(iMallWarePurchaseService.unreceive());
	}

    /**
     * 导出采购信息列表
     */
    @ApiOperation("导出采购信息列表")
    @PreAuthorize("@ss.hasPermi('mall:warePurchase:export')")
    @Log(title = "采购信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<MallWarePurchaseVo> export(@Validated MallWarePurchaseQueryBo bo) {
        List<MallWarePurchaseVo> list = iMallWarePurchaseService.queryList(bo);
        ExcelUtil<MallWarePurchaseVo> util = new ExcelUtil<MallWarePurchaseVo>(MallWarePurchaseVo.class);
        return util.exportExcel(list, "采购信息");
    }

    /**
     * 获取采购信息详细信息
     */
    @ApiOperation("获取采购信息详细信息")
    @PreAuthorize("@ss.hasPermi('mall:warePurchase:query')")
    @GetMapping("/{id}")
    public AjaxResult<MallWarePurchaseVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iMallWarePurchaseService.queryById(id));
    }

    /**
     * 新增采购信息
     */
    @ApiOperation("新增采购信息")
    @PreAuthorize("@ss.hasPermi('mall:warePurchase:add')")
    @Log(title = "采购信息", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody MallWarePurchaseAddBo bo) {
        return toAjax(iMallWarePurchaseService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改采购信息
     */
    @ApiOperation("修改采购信息")
    @PreAuthorize("@ss.hasPermi('mall:warePurchase:edit')")
    @Log(title = "采购信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody MallWarePurchaseEditBo bo) {
        return toAjax(iMallWarePurchaseService.updateByEditBo(bo) ? 1 : 0);
    }

	/**
	 * 保存分配人员
	 */
	@ApiOperation("保存分配人员")
	@Log(title = "保存分配人员", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/updateAssignWarePurchase")
	public AjaxResult<Void> updateAssignWarePurchase(@Validated @RequestBody MallWarePurchaseEditBo bo) {
		return toAjax(iMallWarePurchaseService.updateAssignWarePurchase(bo) ? 1 : 0);
	}

	/**
	 * 保存分配人员
	 */
	@ApiOperation("领取采购单")
	@Log(title = "领取采购单", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PutMapping("/receivePurchase")
	public AjaxResult<Void> receivePurchase(@Validated @RequestBody MallWarePurchaseEditBo bo) {
		return toAjax(iMallWarePurchaseService.receivePurchase(bo) ? 1 : 0);
	}

    /**
     * 删除采购信息
     */
    @ApiOperation("删除采购信息")
    @PreAuthorize("@ss.hasPermi('mall:warePurchase:remove')")
    @Log(title = "采购信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iMallWarePurchaseService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

	/**
	 * 完成采购单
	 */
	@ApiOperation("完成采购单")
	@Log(title = "完成采购单", businessType = BusinessType.UPDATE)
	@RepeatSubmit
	@PostMapping("/finishPurchaseOrder")
	public AjaxResult<Void> finishPurchaseOrder(@Validated @RequestBody MallWarePurchaseEditBo bo) {
		return toAjax(iMallWarePurchaseService.finishPurchaseOrder(bo) ? 1 : 0);
	}
}
